﻿using Autofac;
using Autofac.Extras.CommonServiceLocator;
using Microsoft.Practices.ServiceLocation;
using Topshelf.Sample;

namespace Topshelf.CommonService.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create your container
            var builder = new ContainerBuilder();
            builder.RegisterType<SampleDependency>().As<ISampleDependency>();
            builder.RegisterType<SampleService>();
            var container = builder.Build();

            ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(container));

            HostFactory.Run(c =>
            {
                // Pass it to Topshelf
                //c.UseServiceLocatorContainer();

                c.SetDisplayName("DisplayName");
                c.SetDescription("Description");
                c.SetServiceName("ServiceName");

                c.StartAutomatically();

                c.Service<SampleService>(s =>
                {
                    // Let Topshelf use it
                    s.ConstructUsingServiceLocatorContainer();
                    s.WhenStarted((service, control) => service.Start());
                    s.WhenStopped((service, control) => service.Stop());
                });
            });
        }
    }
}
